(function($) {
	$.getUrlParam = function(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if (r != null) return unescape(r[2]);
		return null;
	}
})(jQuery);



const d = D = {
	queryOne() {
		var id = $.getUrlParam("did");
		$.ajax({
			type: 'get',
			url: url.path + 'dingdan/queryOne',
			data: {
				id: id
			},
			success: function(res) {
				var res = eval("(" + res + ")");
				if (res.code == "200") {
					$('#uid').val(res.data.uid);
					$('#woNumber').text(res.data.dno);
					$('#contactNameDetail').text(res.data.lxrname);
					$('#contactPhoneDetail').text(res.data.lxrphone);
					$('#faultLocationDetail').text(res.data.address);
					$('#faultReasonDetail').text(res.data.description);
					$('#faultImage1').attr('src', res.data.img1);
					$('#faultImage2').attr('src', res.data.img2);
					$('#faultImage3').attr('src', res.data.img3);
					$('#repairDateDetail').text(D.timestampToDate(res.data.date));
					$('#faultLocationDetail').text(res.data.address);
					$('#wname').text(res.data.wname);
					$('#wwname').text(res.data.wname);
					$('#wid').val(res.data.wid);
					$('#wphone').text(res.data.wphone);
					$('#enddate').text(D.timestampToDate(res.data.enddate))
				} else {
					alert(res.msg);
					$('#captchaContainer').data('sliderCaptcha').reset();
					$('#captchaContainer').data('sliderCaptcha').init();
					$('#xyb').attr('disabled', true).css({
						'background-color': '#ccc',
						'cursor': 'not-allowed',
						'border-color': '#ccc'
					});
				}
			}
		});
	},
	timestampToDate(timestamp) {
		const date = new Date(timestamp); // 将时间戳转换为Date对象
		const options = {
			year: 'numeric',
			month: 'long',
			day: 'numeric'
		}; // 定义日期格式
		return new Intl.DateTimeFormat('zh-CN', options).format(date); // 使用Intl.DateTimeFormat进行格式化
	},
	edit(state) {
		var isAdd = confirm("是否确认执行当前操作?")
		if (isAdd) {
			var currentDate = new Date();
			var year = currentDate.getFullYear();
			var month = String(currentDate.getMonth() + 1).padStart(2, '0');
			var day = String(currentDate.getDate()).padStart(2, '0');

			var formattedDate = `${year}-${month}-${day}`;
			$.ajax({
				url: url.path + 'dingdan/edit',
				data: {
					id: $.getUrlParam("did"),
					state: state,
					wid: $('#wid').val(),
					wname: $('#wname').text(),
					wphone: $('#wphone').text(),
					enddate: formattedDate,
					senddate: ""
				},
				type: 'post',
				dataType: 'json',
				success: function(res) {
					if (res.code == 200) {
						alert(res.msg);
						if (state == 7) {
							window.location.href = "yiquxiao.html?did=" + $.getUrlParam("did");
						} else if (state == 0) {
							window.location.href = "yiwancheng.html?did=" + $.getUrlParam("did");
						}
					}
				},
				error: function() {
					alert('修改失败')
				}
			})
		}
	}
}
$(function() {
	D.queryOne();
	$('#approveBtn').click(function() {
		D.edit(0);
	})
	$('#cancelWorkorderBtn').click(function() {
		D.edit(7);
	})

})